package com.vma.interfaceShoes.controller;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.kit.HttpKit;
import com.vma.interfaceShoes.util.ReturnsUtil;
import com.vma.offer.service.CartService;
import com.vma.offer.vo.CartItemVo;
import com.vma.order.service.OrderService;
import com.vma.order.vo.OrderCommitVo;
import net.sf.json.JSONObject;

import java.util.Map;

/**
 * Created by chenzui on 2017/4/13.
 */
public class OrderController extends Controller {

    public void commitFromCart(){
        try{
            String postdata = getPara("cartStr");
            String nbr = OrderService.commit(new Gson().fromJson(postdata, OrderCommitVo.class));
            renderJson(ReturnsUtil.returnSuccess(nbr));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    /**
     * 订单确认页面初始化
     */
    public void confirmInit(){
        try{
            String nbr = getPara("orderNbr");
            renderJson(ReturnsUtil.returnSuccess(OrderService.confirmInit(nbr)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     *  订单确认
     */
    public void confirm(){
        try{
            int orderId = getParaToInt("orderId");
            int addressId = getParaToInt("addressId");
            int userId = getParaToInt("userId");
            String remark = getPara("remark");

            Map result = OrderService.confirm(orderId,addressId,userId,remark);
            if("0".equals(result.get("code").toString())){
                renderJson(ReturnsUtil.returnSuccess("订单确认成功"));
            }else {
                renderJson(ReturnsUtil.returnFail(result.get("code").toString(),"-1"));
            }
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void qryOrderList(){
        try{
            int userId =  getParaToInt("userId");
            int cPage =  getParaToInt("cPage");
            int pSize =  getParaToInt("pSize");
            String status = getPara("status");
            renderJson(ReturnsUtil.returnSuccess(OrderService.qryOrderList("",status,cPage,pSize,"","",userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }


    public void qryOrderDetail(){
        try{
            String status = getPara("orderId");
            renderJson(ReturnsUtil.returnSuccess(OrderService.qryOrderDetail(status)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
    /**
     * 查询订单统计数量
     */
    public void qryOrderCount(){
        try{
            int userId =  getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(OrderService.qryOrderCount(userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * address，邮费，满减
     */
    public void initDataForOrderCommit(){
        try{
            int userId =  getParaToInt("userId");
            renderJson(ReturnsUtil.returnSuccess(OrderService.initDataForOrderCommit(userId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * 订单商品清单
     */
    public void qryOfferListByOrderId(){
        try{
            int orderId =  getParaToInt("orderId");
            renderJson(ReturnsUtil.returnSuccess(OrderService.qryOfferListByOrderId(orderId)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * 更改订单状态
     */
    public void updateStatus(){
        try{
            int orderId =  getParaToInt("orderId");
            String status =  getPara("status");
            OrderService.updateStatus(orderId,status);
            renderJson(ReturnsUtil.returnSuccess("success"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

}
